# -*- coding:utf-8 -*-
import MySQLdb
import ConfigParser
import os

# 读取配置文件

pth = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))

p = os.path.join(pth, "config", "db.cfg")

config = ConfigParser.SafeConfigParser()
config.read(p)
host = config.get('datasource', 'host')
user = config.get('datasource', 'user')
passwd = config.get('datasource', 'passwd')
database = config.get('datasource', 'database')

# 第二期数据库配置
hostV2 = config.get('datasourceV2', 'host')
userV2 = config.get('datasourceV2', 'user')
passwdV2 = config.get('datasourceV2', 'passwd')
databaseV2 = config.get('datasourceV2', 'database')


class MysqlUtils(object):
    '''
        数据库工具类
    '''

    # 获取数据库的连接
    @classmethod
    def get_mysql_connections(cls):
        conn = MySQLdb.connect(host=host, user=user, passwd=passwd, db=database, charset="utf8")
        return conn

    # 获取数据库的连接(第二期)
    @classmethod
    def get_mysql_connections_v2(cls):
        conn = MySQLdb.connect(host=hostV2, user=userV2, passwd=passwdV2, db=databaseV2, charset="utf8")
        return conn
